<div class="content-wrapper">
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('goods-spec-tag');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回特定标签列表'); ?></a>
            <button type="button" class="btn btn-success btn-sm"  data-toggle="modal" onclick="addSpecTagGoods();"><i class="fa fa-plus"></i> <?php echo $this->translate('添加商品')?></button>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $this->translate('设置标签商品'); ?></h3>
                    </div>

                    <div class="box-body">
                        <?php echo $this->partial('layout/messages'); ?>
                        <form class="form-horizontal">
                            <div>
                                <label class="col-sm-1 control-label"><?php echo $this->translate('标签名称'); ?></label>
                                <div class="col-sm-6">
                                    <p class="form-control-static">
                                        <?php echo $this->escapeHtml($this->specTagInfo->getSpecTagName()); ?>
                                    </p>
                                </div>
                            </div>
                        </form>
                    </div>
                    <form method="post" action="<?php echo $this->url('goods-spec-tag', ['action' => 'updateSpecTagGoodsAllSort']);?>" name="edit-all-form" id="edit-all-form">
                    <div class="box-body table-responsive no-padding" id="goodsListBody">
                        <table class="table shop-table">
                            <thead>
                            <tr class="shop-bg-thead">
                                <th width="5%">ID</th>
                                <th width="45%"><?php echo $this->translate('商品名称'); ?></th>
                                <th width="15%"><?php echo $this->translate('排序'); ?></th>
                                <th width="15%"><?php echo $this->translate('商品状态'); ?></th>
                                <th width="15%"><?php echo $this->translate('操作'); ?></th>
                            </tr>
                            </thead>
                            <tbody id="insert-goods">
                            <?php if(isset($this->specTagGoodsList)) foreach ($this->specTagGoodsList as $goodsValue) { ?>
                                <tr id="spec-tag-goods-<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>">
                                    <td><?php echo $goodsValue[0]->getGoodsId(); ?></td>
                                    <td><?php echo $this->escapeHtml($goodsValue['goodsName']); ?></td>
                                    <td><input type="hidden" name="specTagGoodsId[]" value="<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>"><input type="number" min="1" name="specTagGoodsSort<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>" id="specTagGoodsSort<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>" value="<?php echo $this->escapeHtml($goodsValue[0]->getSpecTagGoodsSort()); ?>" class="form-control"></td>
                                    <td><?php echo $this->goodsHelper()->goodsState($goodsValue['goodsState']); ?></td>
                                    <td>
                                        <button type="button" class="btn btn-xs btn-success" onclick="updateSpecTagGoods(<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>);"><i class="fa fa-refresh"></i> <?php echo $this->translate('更新'); ?></button>
                                        <a href="javascript:;" onclick="deleteNotConfirm('<?php echo $this->url('goods-spec-tag', ['action'=>'deleteSpecTagGoods', 'id'=>$goodsValue[0]->getSpecTagGoodsId()]); ?>', 'spec-tag-goods-<?php echo $goodsValue[0]->getSpecTagGoodsId(); ?>');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a>
                                    </td>
                                </tr>
                            <?php } ?>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="4">
                                    <button type="submit" class="btn btn-primary btn-sm"><?php echo $this->translate('排序更新'); ?></button>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                    </form>
                </div>
            </div>
        </div>

    </section>

    <div class="modal fade" id="modalDefault">
        <div class="modal-dialog" style="width: 800px;" id="addGoodsListBody">

        </div>
    </div>

</div>
<script type="text/javascript">
    function addSpecTagGoods() {
        $('#modalDefault').modal('show');
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>', 'addGoodsListBody');
    }
    function searchGoods() {
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>?searchGoodsName='+$('#searchGoodsName').val(), 'addGoodsListBody');
    }
    function addSelectGoods(goodsId) {
        if(goodsId == '') {
            shopMessage('<?php echo $this->translate('请选择需要添加的商品!')?>');
        } else {
            let insertState = true;
            $("input[name='goodsId[]']").each(function () {
                if($(this).val() == goodsId) {
                    insertState = false;
                }
            });
            if(!insertState) {
                return false;
            }
            $.post("<?php echo $this->url('goods-spec-tag', ['action' => 'addSpecTagGoods']);?>", {goodsId: goodsId, specTagId:<?php echo $this->specTagInfo->getSpecTagId(); ?>, specTagCode: "<?php echo $this->specTagInfo->getSpecTagCode(); ?>", templateCode: "<?php echo $this->specTagInfo->getTemplateCode(); ?>"}, function (data) {
                if(data.state == 'ok') {
                    let htmlStr = '<tr id="spec-tag-goods-'+data.result.specTagGoodsId+'">';
                    htmlStr += '<td>'+data.result.goodsId+'</td>';
                    htmlStr += '<td>'+data.result.goodsName+'</td>';
                    htmlStr += '<td><input type="hidden" name="specTagGoodsId[]" value="'+data.result.specTagGoodsId+'"><input type="number" min="1" name="specTagGoodsSort'+data.result.specTagGoodsId+'" id="specTagGoodsSort'+data.result.specTagGoodsId+'" value="'+data.result.specTagGoodsSort+'" class="form-control"></td>';
                    htmlStr += '<td>'+data.result.goodsState+'</td>';
                    htmlStr += '<td><button type="button" class="btn btn-xs btn-success" onclick="updateSpecTagGoods('+data.result.specTagGoodsId+');"><i class="fa fa-refresh"></i> <?php echo $this->translate('更新'); ?></button>&nbsp;<a href="javascript:;" onclick="deleteNotConfirm(\'<?php echo $this->url('goods-spec-tag', ['action'=>'deleteSpecTagGoods']); ?>/'+data.result.specTagGoodsId+'\', \'spec-tag-goods-'+data.result.specTagGoodsId+'\');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a></td>';
                    htmlStr += '</tr>';

                    $('#insert-goods').prepend(htmlStr);
                } else {
                    shopMessage('<?php echo  $this->translate('该商品已经添加!')?>');
                }
            }, 'json');
        }
    }
    function addMoreSelectGoods() {
        $("input[name='selectGoodsId[]']").each(function () {
            if(this.checked) {
                addSelectGoods(this.value);
            }
        });
        $('#modalDefault').modal('hide');
    }

    function updateSpecTagGoods(specTagGoodsId) {
        $.post("<?php echo $this->url('goods-spec-tag', ['action'=> 'updateSpecTagGoods']);?>", {specTagGoodsId:specTagGoodsId, specTagGoodsSort:$('#specTagGoodsSort'+specTagGoodsId).val()}, function (data) {
            shopMessage(data.message);
        }, 'json');
    }
</script>
